<?php
require_once ('../app.render.php');

$function = $_GET['function'];


switch($function){

	
	case "show-stats":
	$orders = $database->select("SELECT * FROM order_sum WHERE rid = ".$_SESSION['pos']." and lid = ".$_SESSION['pos_location']." AND  DATE(date) = DATE('".$_GET['date']."')  AND (pos = 1 or pos = 0) AND status = 1 ORDER by date asc");
							 
						
          if($orders != FALSE){
            for($i=0; $i<count($orders); $i++){
              $subtotal +=  $orders[$i]['subtotal'];
              $tax +=       $orders[$i]['tax'];	
              $tips +=      $orders[$i]['tips'];
              $total +=	    $orders[$i]['total'];
          
          
              if($orders[$i]['delivery'] != "No"){
                $delivery += 1;	
              }else{
                $pickup += 1;	
              }
            }
          }
						
						
						
	echo '<table width="100%" border="0" cellspacing="3" cellpadding="3">
  <tr>
    <td>Sales: </td>
    <td>'.price_format($subtotal).'</td>
  </tr>
  <tr>
    <td>Taxes:</td>
    <td>'.price_format($tax).'</td>
  </tr>
  <tr>
    <td>Totals:</td>
    <td>'.price_format($total).'</td>
  </tr>
  <tr>
    <td>Deliveries: </td>
    <td>'.$delivery.'</td>
  </tr>
    <tr>
    <td>Pickups: </td>
    <td>'.$pickup.'</td>
  </tr>
</table>';
	
						
						
	
	break;
	
	
case "delete-order":
  $database->delete("order_sum", "id= ".$_POST['id']."");
break;

case "change-status":
  $u['pos'] = $_POST['status'];
  $id = $_POST['id'];
  $database->update_array("order_sum", $u, "id = $id");
break;
	
case "load-old-orders":
	
	$orders = $database->select("SELECT * FROM order_sum WHERE rid = ".$_SESSION['pos']." and lid = ".$_SESSION['pos_location']." AND  DATE(date) = DATE('".$_GET['date']."')  AND (pos = 2) AND status = 1 ORDER by date desc");
						 
		
	if($orders != FALSE){
          for($i=0; $i<count($orders); $i++){
	  $orders[$i]['user'] = $database->select("SELECT * FROM users where id = ".$orders[$i]['uid']."");			
          $time_dif = time() - strtotime($orders[$i]['date']);
          $time_dif2 = $time_dif;
          $time_dif = $time_dif / 60;
          $time_dif = round($time_dif);
          echo '
			<p class="separator"><h2><a href="javascript:viewOrder('.$orders[$i]['id'].')">'.$orders[$i]['user'][0]['name'].' '.$orders[$i]['user'][0]['last_name'].'</a></h2>
			Order Total: '.price_format($orders[$i]['total']).'<br>
			Order Time:  '.date('h:i A', strtotime($orders[$i]['date'])).' -  <em>'.$time_dif.'  Minutes Ago 
			</p>';
          }
        }
	
break;
	
case "load-orders":
  $orders = $database->select("SELECT * FROM order_sum WHERE rid = ".$_SESSION['pos']." and lid = ".$_SESSION['pos_location']." AND  DATE(date) = DATE('".$_GET['date']."')  AND (pos = 1 or pos = 0) AND status = 1 ORDER by date asc");
        
  if($orders != FALSE){
    for($i=0; $i<count($orders); $i++){
      $orders[$i]['user'] = $database->select("SELECT * FROM users where id = ".$orders[$i]['uid']."");
      $time_dif = time() - strtotime($orders[$i]['date']);
      $time_dif2 = $time_dif;
      $time_dif = $time_dif / 60;
      $time_dif = round($time_dif);
      $play_sound = "";
      if($time_dif2 <6){
	$play_sound = '	<embed src="alert.mp3" autostart=true loop=false height="1" width="1">';	
      }

      echo '
        <tr>
          <td><a href="javascript:viewOrder('.$orders[$i]['id'].')">'.$orders[$i]['user'][0]['name'].' '.$orders[$i]['user'][0]['last_name'].'</a></td>
          <td>'.date('h:i A', strtotime($orders[$i]['date'])).' -  <em>'.$time_dif.'  Minutes Ago '.$play_sound.' </em></td>
          <td>'.$orders[$i]['delivery'].'</td>
          <td>'.$orders[$i]['payment'].'</td>
          <td>'.price_format($orders[$i]['total']).'</td>
          <td>   <a href="javascript:viewOrder('.$orders[$i]['id'].')">';
          if ($orders[$i]['pos'] == 0){
            echo ' <span class="tag red">Not Viewed</span>';
          }

          if ($orders[$i]['pos'] == 1){
            echo '  <span class="tag green">Viewed</span>';
          }

      echo '
            </a>
          </td>
          <td>
          <ul class="actions">
            <li><a class="view" href="javascript:viewOrder('.$orders[$i]['id'].')" title="View Item" rel="tooltip">view</a></li>

            <li><a class="delete" href="javascript:deleteOrder('.$orders[$i]['id'].')" title="Delete Item" rel="tooltip">delete</a></li>
          </ul>
          </td>
          </tr>';
    }
  }
break;

case "show-order":

  $results = $database->select("SELECT * FROM order_sum WHERE id = ".$_GET['oid']);
  if($results[0]['pos'] == 0){
    $us['pos'] = 1;
    $database->update_array("order_sum", $us, "id = ".$_GET['oid']."");
    $results[0]['pos'] = 1;
  }

  $results_user = $database->select("SELECT * FROM users where id = ".$results[0]['uid']);
  $results_location = $database->select("SELECT * FROM locations where id = ".$results[0]['lid']);
  $results_inv = $database->select("SELECT * FROM order_inv where oid = ".$results[0]['id']);

  if(isset($_GET['status'])){
    if ($_GET['status'] != ""){
      $up['status'] = $_GET['status'];
      $database->update_array('order_sum', $up, "id=".$_GET['oid']."");
      $st = $database->select("SELECT * FROM order_status where id = '".$_GET['status']."'");
      
      $m= new Mail; // create the mail
      $m->From ($config[0]['email']);
      $m->To ($results_user[0]['email']);
      $m->Subject (stripslashes($st[0]['subject']));		
      $m->Body (stripslashes($st[0]['email']));	// set the body	
      $m->Send ();	// send the mail
      
      echo '
      <script type="text/javascript">
      window.location = "index.php?edit=orders&updated=1&oid='. $_GET['oid'].'";
      </script>
      ';
    }    
  }


  $delivery_add = "";
  if($results[0]['delivery'] == "Yes"){
    $delivery_add = '<h4>Delivery Address</h4>
    '.stripslashes($results[0]['delivery_address1']).'  '.stripslashes($results[0]['delivery_address2']).'
    <br>'.stripslashes($results[0]['delivery_city']).',	'.stripslashes($results[0]['delivery_state']).'	'.stripslashes($results[0]['delivery_zip']).'			
    ';
  }

  $total =  $results[0]['subtotal'] +   $results[0]['tips'] +  $results[0]['tax'] + $results[0]['delivery_fee'];

  if($results[0]['cid'] != ""){
    $couponID =$results[0]['cid']; 
    $coupon = '<tr>
    <td>Coupon Used:</td>
    <td> '.$couponID.' </td>
    </tr>';	

    $coupons = $database->select("SELECT * from coupons where code = '" . $couponID . "'");
    $discount = $coupons[0]['discount'];
    $discountType = $coupons[0]['type'];
    $discountMinimum = $coupons[0]['minimum'];
    $discount = getDiscountAmount($total, $discount, $discountType, $discountMinimum); 
    $total = $total - $discount; 
  }

 
  echo '
  <div style="text-align:right;font-size:.8em;padding:5px;"><a style="text-decoration:none" href="javascript:printMe();">
  <img src="template/img/icons/printer.png"> Print Order</a></div>
  <div id="printme">
  <table cellspacing="0" cellpadding="2" border="0" class="table_form" style="text-align:left" width="98%">


  <tr>
  <td>Customer Name:</td>
  <td><strong>'.$results_user[0]['name'].'   '.$results_user[0]['last_name'].'</strong></td>
  </tr>
  <tr>
  <td>Order Date:</td>
  <td> '. date("F d Y h:i: A", strtotime($results[0]['date'])).' </td>
  </tr>
  <tr style="background-color:CCC">
  <td>Order Total:</td>
  <td>'.@money_format('%i',floatval($total)) .'</td>
  </tr>


  <tr>
  <td>Delivery:</td>
  <td> '.$results[0]['delivery'].' </td>
  </tr>
  <tr style="background-color:CCC">
  <td>Payment:</td>
  <td>'.$results[0]['payment'].'</td>
  </tr>
  '.$coupon.'
  <tr>

  <tr>

  <td>Status</td>';

  $selectedpos1 = "";
  if($results[0]['pos'] == 0){
    $selectedpos1 = ' selected="selected"';
  }
  $selectedpos2 = "";
  if($results[0]['pos'] == 1){
    $selectedpos2 = ' selected="selected"';
  }
  $selectedpos3 = "";
  if($results[0]['pos'] == 2){
    $selectedpos3 = ' selected="selected"';
  }

  echo '<td><select name="pos" id="status" onchange="changeStatus('.$results[0]['id'].');">

  <option value="0" '.$selectedpos1.' >Not Viewed</option>
  <option value="1" '.$selectedpos2.' >Processing</option>
  <option value="2" '.$selectedpos3.'>Complete</option>
  </select></td>

  </tr>



  <td colspan="2" style="background-color:#CCC;color:#FFF;font-size:1.4em;font-weight:bold">Customer Info</td>

  </tr>

  <tr>
  <td  >
  <h4>Billing Address</h4>
  '.stripslashes($results[0]['address1']).'  '.stripslashes($results[0]['address2']).'
  <br>'.stripslashes($results[0]['city']).',	'.stripslashes($results[0]['state']).'	'.stripslashes($results[0]['zip']).'			

  </td>
  <td>
  '.$delivery_add.'
  </td>
  </tr>
  <tr style="background-color:CCC">
  <td>Comments:</td>
  <td>'.$results[0]['comments'] .'</td>
  </tr>
  <tr>
  <td colspan="2" style="background-color:#CCC;color:#FFF;font-size:1.4em;font-weight:bold">Order Details</td>

  </tr>


  <tr>

  <tr>

  <td  colspan="3"><table width="100%" border="0" cellspacing="3" cellpadding="3">';

  for($i=0; $i<count($results_inv); $i++){

  $price = $results_inv[$i]['item_price'] *$results_inv[$i]['item_quantity'];
  echo '					
  <tr>
  <td style="border:none" width="400"><b> '.$results_inv[$i]['item_quantity'].' x Item:</b>  '.$results_inv[$i]['item_name'].' </td>
  <td  style="border:none"><b>Price:</b>  '.@money_format('%i', floatval($results_inv[$i]['item_price'])).' </b> <b style="margin-left:120px">Total:</b>  '.@money_format('%i',floatval($price)).'</td>
  </tr>
  <tr>
  <td colspan="2" style="border-bottom:1px dotted #CCC;padding-bottom:10px"> '.htmlspecialchars_decode($results_inv[$i]['item_description']).' </td>

  </tr>

  ';	

  }

  echo '</table>

    <div style="text-align:right">
      <h3>
        Sub Total:'.@money_format('%i', floatval($results[0]['subtotal'])) .' <br>
        Tip: '.@money_format('%i', floatval($results[0]['tips'])) .' <br>
        Tax: '.@money_format('%i', floatval($results[0]['tax'])) .' <br>
        Delivery fee: '.@money_format('%i', floatval($results[0]['delivery_fee'])) .' <br>
        Coupon: -'.@money_format('%i', floatval($discount)).' <br>
        <span style="color:green"> Total:   '.@money_format('%i',floatval($total)) .'</span>
      </h3>
    </div>
  </td>
  </tr>
  </tr>
  </table>
  </div>

  ';


break;	
	
}

?>
